package com.controller;

import com.api.CommonResult;
import com.entity.Type;
import com.entity.Users;
import com.entity.table.ProductsTableDef;
import com.entity.table.UsersTableDef;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.service.TypeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.entity.Products;
import com.service.ProductsService;

import java.io.Serializable;
import java.util.List;

/**
 * 商品信息表 控制层。
 *
 * @author admin
 * @since 2025-05-26
 */
@Controller
@RequestMapping("/admin/products")
public class ProductsController {

    @Autowired
    private ProductsService productsService;

    /**
     * 添加商品信息表。
     *
     * @param products 商品信息表
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
//    @PostMapping("save")
//    public boolean save(@RequestBody Products products) {
//        return productsService.save(products);
//    }

    /**
     * 根据主键删除商品信息表。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    public boolean remove(@PathVariable Serializable id) {
        return productsService.removeById(id);
    }

    /**
     * 根据主键更新商品信息表。
     *
     * @param products 商品信息表
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
//    @PutMapping("update")
//    public boolean update(@RequestBody Products products) {
//        return productsService.updateById(products);
//    }

    /**
     * 查询所有商品信息表。
     *
     * @return 所有数据
     */
//    @GetMapping("list")
//    public List<Products> list() {
//        return productsService.list();
//    }

    /**
     * 根据商品信息表主键获取详细信息。
     *
     * @param id 商品信息表主键
     * @return 商品信息表详情
     */
    @GetMapping("getInfo/{id}")
    public Products getInfo(@PathVariable Serializable id) {
        return productsService.getById(id);
    }

    /**
     * 分页查询商品信息表。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    public Page<Products> page(Page<Products> page) {
        return productsService.page(page);
    }

    @Autowired
    private TypeService typeService;

    @GetMapping(value = "/List")
    public String goodList(ModelMap model , @RequestParam(value = "productName",defaultValue = "")String goodname,
                           @RequestParam(value = "p",defaultValue = "1")int p,
                           @RequestParam(value = "pageSize", defaultValue = "10")int pageSize){
        try {
            QueryWrapper queryWrapper = QueryWrapper.create().where(ProductsTableDef.PRODUCTS.PRODUCT_NAME.like(goodname));
            Page<Products> page = new Page<>();
            page.setPageSize(pageSize);
            page.setPageNumber(p);
            Page<Products> pageInfo = productsService.getMapper().paginateWithRelations(page, queryWrapper);
            model.addAttribute("page", pageInfo);
            model.addAttribute("total", pageInfo.getTotalRow());
        } catch (Exception e){
            e.printStackTrace();
        }
        return "back/goods-list";
    }

    @GetMapping(value = "/addView")
    public String addView(Model model){
        List<Type> typeList = typeService.list();
        model.addAttribute("typeList",typeList);
        return "back/goods-add";}

    @PostMapping("save")
    @ResponseBody
    public CommonResult save(Products bGoods){
        try{
            boolean r = productsService.save(bGoods);
            return CommonResult.success(r);
        }catch (Exception e){
            e.printStackTrace();
            String msg = bGoods.getProductName()+"已存在";
            return CommonResult.failed(msg);
        }
    }
    @GetMapping(value = "updateView")
    public String updateView(int id , Model model){
        List<Type> typeList = typeService.list();
        Products bGood = productsService.getById(id);
        model.addAttribute("typeList",typeList);
        model.addAttribute("bGood",bGood);
        return "back/goods-edit";
    }
    @PostMapping("update")
    @ResponseBody
    public CommonResult update(Products bGoods){
        try{
            boolean r = productsService.updateById(bGoods);
            return CommonResult.success(r);
        }catch (Exception e){
            e.printStackTrace();
            String msg = bGoods.getProductName()+"已存在";
            return CommonResult.failed(msg);
        }
    }
    @PostMapping("del")
    @ResponseBody
    public CommonResult del(Integer id){
        try{
            boolean r = productsService.removeById(id);
            return CommonResult.success(r);
        }catch (Exception e){
            e.printStackTrace();

            return CommonResult.failed(e.getMessage());
        }
    }

}
